<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\utils\XHtml;

/* @var $this yii\web\View */
/* @var $model common\models\Config */

$this->title = $model->isNewRecord ? '新建自定义' : '更新自定义';
$this->params['breadcrumbs'][] = ['label' => '自定义管理', 'url' => ['custom']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="page-create">
    <div class="box box-primary">
        <div class="box-body">

            <?= $this->render('/site/error-summary', ['model' => $model]) ?>

            <?php $form = ActiveForm::begin([
                'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                'fieldConfig' => [
                    'template' => "{label}\n<div class=\"col-sm-7\">{input}</div>\n<div class=\"col-sm-3\">{hint}\n{error}</div>",
                    'labelOptions' => ['class ' => 'col-sm-2 control-label'],
                ]
            ]); ?>
            <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
            <?= $form->field($model, 'variable')->textInput(['maxlength' => true]) ?>
            <?= $form->field($model, 'type')->dropDownList(['input' => '单行文本', 'textarea' => '多行文本','file'=>'图片']) ?>
            <?php if ($model->type == 'input') { ?>
                <?= $form->field($model, 'value') ?>
            <?php } else if($model->type=='file'){ ?>
                <?= $form->field($model, 'value')->fileInput(['accept' => 'image/*','name'=>'file']) ?>
                <?php if ($model->value) { ?>
                    <?= Html::beginTag('div', ['class' => 'form-group']) ?>
                    <?= Html::label('已上传图片', '', ['class' => 'col-sm-2 control-label']) ?>
                    <?= Html::beginTag('div', ['class' => 'col-sm-10']) ?>
                    <?= Html::img(Yii::getAlias('@frontendUrl') . $model->value, ['width' => '200']) ?>
                    <?= Html::endTag('div') ?>
                    <?= Html::endTag('div') ?>
                <?php } ?>
            <?php } else{?>
                <?= $form->field($model, 'value')->textarea() ?>
            <?php }?>
            <?= $form->field($model, 'scode')->hiddenInput()->label(false) ?>


            <div class="form-group">
                <label class="col-sm-2 control-label"></label>
                <?= XHtml::submitButton($model->isNewRecord ? '创建' : '更新') ?>&nbsp;&nbsp;
                <?= XHtml::backButton() ?>&nbsp;&nbsp;
                <?= XHtml::resetButton() ?>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>
<?php
$script = <<<JS
    var inputType = '<input type="text" id="config-value" class="form-control" name="Config[value]">';
    var textareaType = '<textarea id="config-value" class="form-control" name="Config[value]"></textarea>';
    var imgType='<input type="file" id="config-value" name="file">';
    $("#config-type").change(function() {
        if ($(this).val()=='textarea'){
            $("#config-value").parent().html(textareaType);
        }else if($(this).val()=='file'){
            $("#config-value").parent().html(imgType);
        }else{
            $("#config-value").parent().html(inputType);
        }
    });
JS;
$this->registerJs($script);
?>
